/*
 *  txdnet.cn tonydon
 * 
 */
package cn.txdnet.txcms.service;

import cn.txdnet.txcms.dao.EventsDAO;
import cn.txdnet.txcms.domain.Events;
import cn.txdnet.txcms.dto.PageDTO;
import cn.txdnet.txcms.query.EventsQuery;
import cn.txdnet.txcms.query.ViewEventsQuery;
import cn.txdnet.txweb.basedao.FacadeProxy;
import cn.txdnet.txweb.utils.StringUtil;
import java.util.HashMap;
import java.util.Map;
import txdnet.util.StaticMark;

/**
 *
 * @author txdnet
 */
public class EventsService {

    /**
     * 保存活动信息
     *
     * @param event
     * @return
     */
    public static boolean make(Events event) {
        EventsDAO dao = FacadeProxy.getManualCommitProxy(EventsDAO.class);
        return (dao.insertEvents(event) > 0 ? true : false);
    }

    /**
     * 更新活动信息
     *
     * @param event
     * @return
     */
    public static boolean update(Events event) {
        EventsDAO dao = FacadeProxy.getManualCommitProxy(EventsDAO.class);
        return (dao.updateEvents(event) > 0 ? true : false);
    }

    /**
     * 批量删除活动信息
     *
     * @param ids
     * @return
     */
    public static int deleteBatch(String ids) {
        EventsDAO dao = FacadeProxy.getManualCommitProxy(EventsDAO.class);
        return dao.deleteEvents(StringUtil.parseStrToIntArray(ids, StaticMark.STR_LINE));
    }

    /**
     * 批量更新发布状态
     *
     * @param ids
     * @param pubstate
     * @return
     */
    public static int updatePubstateBatch(String ids, byte pubstate) {
        Map<String, Object> m = new HashMap<String, Object>(2);
        m.put("ida", StringUtil.parseStrToIntArray(ids, StaticMark.STR_LINE));
        m.put("pubstate", pubstate);
        EventsDAO dao = FacadeProxy.getManualCommitProxy(EventsDAO.class);
        return dao.updatePubstate(m);
    }

    /**
     * 批量更新信息频道和类目信息
     *
     * @param ids
     * @param cid
     * @param ctp
     * @return
     */
    public static int updateChannelAndCatalogBatch(String ids, byte channeltype, int cid, String ctp) {
        Map<String, Object> m = new HashMap<String, Object>(4);
        m.put("ida", StringUtil.parseStrToIntArray(ids, StaticMark.STR_LINE));
        m.put("cid", cid);
        m.put("ctp", ctp);
        m.put("channeltype", channeltype);
        EventsDAO dao = FacadeProxy.getManualCommitProxy(EventsDAO.class);
        return dao.updateChannelAndCatalog(m);
    }

    /**
     * 批量更新发布时间
     *
     * @param ids
     * @param pubtime
     * @return
     */
    public static int updatePubTimeBatch(String ids, long pubtime) {
        Map<String, Object> m = new HashMap<String, Object>(2);
        m.put("ida", StringUtil.parseStrToIntArray(ids, StaticMark.STR_LINE));
        m.put("pubtime", pubtime);
        EventsDAO dao = FacadeProxy.getManualCommitProxy(EventsDAO.class);
        return dao.updatePubTime(m);
    }

    /**
     * 批量更新截止时间
     *
     * @param ids
     * @param pubtime
     * @return
     */
    public static int updateEndTimeBatch(String ids, long endtime) {
        Map<String, Object> m = new HashMap<String, Object>(2);
        m.put("ida", StringUtil.parseStrToIntArray(ids, StaticMark.STR_LINE));
        m.put("endtime", endtime);
        EventsDAO dao = FacadeProxy.getManualCommitProxy(EventsDAO.class);
        return dao.updateEndTime(m);
    }

    /**
     * 更具查询条件得到记录
     *
     * @param query
     * @return
     */
    public static PageDTO queryByFactor(EventsQuery query) {
        EventsDAO dao = FacadeProxy.getAutoCommitProxy(EventsDAO.class);
        return dao.queryByFactor(query);
    }

    /**
     * 查询活动信息用于编辑表单初始化
     *
     * @param id
     * @return
     */
    public static Events queryEventsForEdit(int id) {
        EventsDAO dao = FacadeProxy.getAutoCommitProxy(EventsDAO.class);
        return dao.queryEventsForEdit(id);
    }

    /**
     * 前端活动查询详情
     *
     * @param id
     * @return
     */
    public static Events queryEventsForView(int id) {
        ViewEventsQuery query = new ViewEventsQuery();
        query.setId(id);
        query.filter();
        EventsDAO dao = FacadeProxy.getAutoCommitProxy(EventsDAO.class);
        return dao.queryEventsForView(query);
    }

    /**
     * 累加信息查看次数
     *
     * @param infoid
     * @return
     */
    public static boolean increaseHitNum(int id) {
        EventsDAO dao = FacadeProxy.getManualCommitProxy(EventsDAO.class);
        return (dao.increaseHitNum(id) > 0 ? true : false);
    }
}